layui.use(['form','element','layer','jquery'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        element = layui.element;
    $ = layui.jquery;

    //回显用户个人信息
    var _ticket = $.cookie("token");
    if(_ticket != null && _ticket !== ""){
        $.ajax({
            url : "http://localhost:8083/user/token/" + _ticket,//单点登录链接
            dataType : "jsonp",
            type : "GET",
            success : function(data){
                if (data.code == 200) {
                    username = data.data.user.loginName;
                } else {
                    layer.confirm('用户登录已过期，请中重新登录', {icon: 3, title: '提示信息'}, function (tips) {});
                    window.location.href = "http://localhost:8083/login";
                }
            }
        });
    }

    //采集设备数
    $.get("/equipment/list",function (data) {
        $("#col-equip-count").html(data.count);
    });
    //控制设备数量
    $.get("/ctrlequipment/list",function (data) {
        $("#ctrcol-equip-count").html(data.count);
    });
    //场景数量
    $.get("/scene/list",function (data) {
        $("#scene-count").html(data.count);
    });

    //饼状线图
    //采集设备分类饼状图
    var pieChart1 = echarts.init(document.getElementById('pie-chart1'));
    var pieoption1 = {
        title : {
            text: '采集设备数量分布',
            subtext: ' ',
            x:'center'
        },
        tooltip : {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            orient: 'vertical',
            left: 'left',
            data: [ "环境采集设备-ZK03", "监控设备" ]
        },
        series : [
            {
                name: '采集设备数量',
                type: 'pie',
                radius : '55%',
                center: ['50%', '60%'],
                data:[
                    { value: 1, name: "环境采集设备-ZK03" },
                    { value: 1, name: "监控设备" },
                ],
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };
    getPieoPtionData_1();
    function getPieoPtionData_1() {
        $.get("/equipment/type/list",function (data) {
            //获取分类数据
            var legend = {
                orient: 'vertical',
                left: 'left',
                data: []
            };
            var series =  [
                {
                    name: '采集设备数量',
                    type: 'pie',
                    radius : '55%',
                    center: ['50%', '60%'],
                    data:[],
                    itemStyle: {
                        emphasis: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ];
            $.each(data.data,function (i, ele) {
                legend.data.push(ele.name);
                var obj = {value:0, name: ele.name};
                //获取分类的设备数据
                $.ajax({
                    url : "/equipment/query?typeId=" + ele.id,
                    async:false,
                    success: function (data) {
                        if (data.count > 0) {
                            obj.value = data.count;
                            series[0].data.push(obj);
                        }
                    }
                });
            });
            pieoption1.legend = legend;
            pieoption1.series = series;
            pieChart1.setOption(pieoption1);
        });
    }

    //控制设备
    var pieChart2 = echarts.init(document.getElementById('pie-chart2'));
    var pieoption2 = {
        title : {
            text: '控制设备数量分布',
            subtext: ' ',
            x:'center'
        },
        tooltip : {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            orient: 'vertical',
            left: 'left',
            data: ['分类1','分类2','分类3','分类4','分类5']
        },
        series : [
            {
                name: '访问来源',
                type: 'pie',
                radius : '55%',
                center: ['50%', '60%'],
                data:[
                    {value:335, name:'分类1'},
                    {value:310, name:'分类2'},
                    {value:234, name:'分类3'},
                    {value:135, name:'分类4'},
                    {value:1548, name:'分类5'}
                ],
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };
    getPieoPtionData_2();
    function getPieoPtionData_2() {
        $.get("/ctrlequipment/type/list",function (data) {
            //获取分类数据
            var legend = {
                orient: 'vertical',
                left: 'left',
                data: []
            };
            var series =  [
                {
                    name: '控制设备数量',
                    type: 'pie',
                    radius : '55%',
                    center: ['50%', '60%'],
                    data:[],
                    itemStyle: {
                        emphasis: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ];
            $.each(data.data,function (i, ele) {
                legend.data.push(ele.name);
                var obj = {value:0, name: ele.name};
                //获取分类的设备数据
                $.ajax({
                    url : "/ctrlequipment/query?typeId=" + ele.id,
                    async:false,
                    success: function (data) {
                        if (data.count > 0) {
                            obj.value = data.count;
                            series[0].data.push(obj);
                        }
                    }
                });
            });
            pieoption2.legend = legend;
            pieoption2.series = series;
            pieChart2.setOption(pieoption2);
        });
    }

    //总设备数据统计
    var pieChart3 = echarts.init(document.getElementById('pie-chart3'));
    var pieoption3 = {
        title : {
            text: '总设备数量分布',
            subtext: ' ',
            x:'center'
        },
        tooltip : {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            orient: 'vertical',
            left: 'left',
            data: ['采集设备数','控制设备数','采集设备故障数','控制设备故障数','网络设备数']
        },
        series : [
            {
                name: '设备数量',
                type: 'pie',
                radius : '55%',
                center: ['50%', '60%'],
                data:[
                    {value:335, name:'采集设备数'},
                    {value:310, name:'控制设备数'},
                    {value:234, name:'采集设备故障数'},
                    {value:135, name:'控制设备故障数'},
                    {value:1548, name:'网络设备数'}
                ],
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };
    getPieoPtionData_3();
    function getPieoPtionData_3() {
        //获取分类数据
        var series =  [
            {
                name: '设备数量',
                type: 'pie',
                radius : '55%',
                center: ['50%', '60%'],
                data:[],
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ];
        //采集设备数量
        $.ajax({
            url : "/equipment/list",
            async:false,
            success: function (data) {
                var obj = {value:0, name: "采集设备数"};
                obj.value = data.count;
                series[0].data.push(obj);
            }
        });
        //控制设备数量
        $.ajax({
            url : "/ctrlequipment/list",
            async:false,
            success: function (data) {
                var obj = {value:0, name: "控制设备数"};
                obj.value = data.count;
                series[0].data.push(obj);
            }
        });
        //采集设备故障数
        $.ajax({
            url : "/equipment/status/query?runningState=4",
            async:false,
            success: function (data) {
                var obj = {value:0, name: "采集设备故障数"};
                obj.value = data.count;
                series[0].data.push(obj);
            }
        });
        //控制设备故障数
        $.ajax({
            url : "/ctrlequipment/status/query?ctrlEquipmentStatus=4",
            async:false,
            success: function (data) {
                var obj = {value:0, name: "控制设备故障数"};
                obj.value = data.count;
                series[0].data.push(obj);
            }
        });
        //网络设备数
        var obj = {value:0, name: "网络设备数"};
        series[0].data.push(obj);
        pieoption3.series = series;
        pieChart3.setOption(pieoption3);
    }

    //折线图
    var lineChart = echarts.init(document.getElementById('line-chart'));
    lineChart.showLoading();//加载动画
    var lineOption = {
        title: {
            text: '采集数据量统计图'
        },
        color:['#91c7ae'],//,'#d48265','#91c7ae'
        tooltip : {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
                label: {
                    backgroundColor: '#6a7985'
                }
            }
        },
        legend: {
            data:['近期采集数据量']
        },
        toolbox: {
            feature: {
                saveAsImage: {}
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis : [
            {
                type : 'category',
                boundaryGap : false,
                data : ['周一','周二','周三','周四','周五','周六','周日']
            }
        ],
        yAxis : [
            {
                type : 'value',
                axisLine: {
                    lineStyle: {
                        // 设置y轴颜色
                        color: '#87CEFA'
                    }
                }
            }
        ],
        series : [
            {
                name:'近期采集数据量',
                type:'line',
                stack: '总量',
                label: {
                    normal: {
                        show: true,
                        position: 'top'
                    }
                },
                areaStyle: {normal: {}},
                data:[820, 932, 901, 934, 1290, 1330, 1320]
            }
        ]
    };
    getlineOptionData();
    function getlineOptionData() {
        $.get("/search/datarecord/page?page=0&limit=5000",function (data) {
            //x轴数据
            var xAxis = [
                {
                    type : 'category',
                    boundaryGap : false,
                    axisLine: {
                        lineStyle: {
                            // 设置x轴颜色
                            color: '#912CEE'
                        }
                    },
                    data : []
                }
            ];
            var series = [
                {
                    name:'近期采集数据量',
                    type:'line',
                    stack: '总量',
                    label: {
                        normal: {
                            show: true,
                            position: 'top'
                        }
                    },
                    smooth:true,
                    areaStyle: {normal: {}},
                    itemStyle: {normal: {areaStyle: {type: 'default'}}},
                    data:[]
                }
            ];
            var dateArr = [];
            var dateStrArr = [];

            $.each(data.data,function (i, ele) {
                var date = new Date(ele.acquisitionTime);
                var dateStr = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + date.getHours() + ":00:00";
                if (contains(dateStrArr,dateStr) != -1) {
                    dateArr.unshift(ele.acquisitionTime);
                    xAxis[0].data.unshift(dateStr);
                    dateStrArr.unshift(dateStr);
                }
            });
            for (var i = 0;i < dateArr.length;i++) {
                var nextDate = new Date(dateArr[i] + 60*60*1000);//24*
                var dateCount = 0;
                $.each(data.data,function (j, ele) {
                    if (ele.acquisitionTime >= dateArr[i] && ele.acquisitionTime <= nextDate) {
                        dateCount++;
                    }
                });
                series[0].data.push(dateCount);
            }
            lineOption.xAxis = xAxis;
            lineOption.series = series;
            lineChart.hideLoading();//关闭加载动画
            lineChart.setOption(lineOption);
        });

    }

    //判断数组中是否存在元素
    function contains(arr, obj) {
        if (arr.length != 0) {
            for (var i = 0;i < arr.length;i++) {
                if(arr[i] == obj) {
                    return -1;
                }
            }
        }
        return 0;
    }
    //柱状图
    /*var zhuChart = echarts.init(document.getElementById('zhu-chart'));
    var option3 = {
        xAxis: {
            type: 'category',
            data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        },
        yAxis: {
            type: 'value'
        },
        series: [{
            data: [120, 200, 150, 80, 70, 110, 130],
            type: 'bar'
        }]
    };
    zhuChart.setOption(option3);*/


});